using System;
using UnityEngine;

[Serializable]
[AddComponentMenu("Camera-Control/Key Mouse Orbit")]
public class KeyboardOrbit : MonoBehaviour
{
	public Transform target;

	public float distanceMin;

	public float distanceMax;

	public float distanceInitial;

	public float scrollSpeed;

	public float xSpeed;

	public float ySpeed;

	public int yMinLimit;

	public int yMaxLimit;

	private float x;

	private float y;

	private float distanceCurrent;

	public KeyboardOrbit()
	{
		distanceMin = 10f;
		distanceMax = 15f;
		distanceInitial = 12.5f;
		scrollSpeed = 1f;
		xSpeed = 250f;
		ySpeed = 120f;
		yMinLimit = -20;
		yMaxLimit = 80;
	}

	public void Start()
	{
		Vector3 eulerAngles = transform.eulerAngles;
		x = eulerAngles.y;
		y = eulerAngles.x;
		distanceCurrent = distanceInitial;
		if ((bool)GetComponent<Rigidbody>())
		{
			GetComponent<Rigidbody>().freezeRotation = true;
		}
	}

	public void LateUpdate()
	{
		if ((bool)target)
		{
			x += UnityEngine.Input.GetAxis("Horizontal") * xSpeed * 0.02f;
			y -= UnityEngine.Input.GetAxis("Vertical") * ySpeed * 0.02f;
			distanceCurrent -= UnityEngine.Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
			distanceCurrent = Mathf.Clamp(distanceCurrent, distanceMin, distanceMax);
			y = ClampAngle(y, yMinLimit, yMaxLimit);
			Quaternion rotation = Quaternion.Euler(y, x, 0f);
			Vector3 position = rotation * new Vector3(0f, 0f, 0f - distanceCurrent) + target.position;
			transform.rotation = rotation;
			transform.position = position;
		}
	}

	public static float ClampAngle(float angle, float min, float max)
	{
		if (!(angle >= -360f))
		{
			angle += 360f;
		}
		if (!(angle <= 360f))
		{
			angle -= 360f;
		}
		return Mathf.Clamp(angle, min, max);
	}

	public void Main()
	{
	}
}
